﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ForumsBrowser.BusinessObjects;
using ForumsBrowser.ObjectGateway;
using System.ComponentModel;

namespace ForumsBrowser
{
    /// <summary>
    /// Interaction logic for ForumsView.xaml
    /// </summary>
    public partial class AvailableForumsView : UserControl
    {
        public event EventHandler<ForumSelectedEventArgs> ForumSelected;
        public event EventHandler<ForumSubscribeEventArgs> ForumSubscribed;
        public event EventHandler<ForumSubscribeEventArgs> ForumUnsubscribed;
        public event EventHandler<ViewSelectedForumsEventArgs> FinishedLoading;
        

        public AvailableForumsView()
        {
            InitializeComponent();
            this.Loaded += AvailableForumsView_Loaded;
        }

        void AvailableForumsView_Loaded(object sender, RoutedEventArgs e)
        {
            BindData(false);
        }
        
        public void BindData(bool forcerefresh)
        {
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                List<ForumCategory> availableForums = Gateway.AvailableForumsGet(forcerefresh);
                stackPanel1.Children.Clear();
                foreach (var category in availableForums)
                {
                    TreeViewItem item = new TreeViewItem { Header = category.CategoryName, Foreground = Brushes.White };

                    foreach (var forum in category.Forums)
                    {
                        TreeViewItem forumItem = new TreeViewItem { Header = forum.LongName, Tag = forum.ShortName };
                        forumItem.Selected += TreeViewItem_Selected;
                        item.HorizontalContentAlignment = HorizontalAlignment.Left;


                        StackPanel panel = new StackPanel();
                        panel.Orientation = Orientation.Horizontal;
                        CheckBox cb = new CheckBox();
                        cb.Tag = forum;
                        cb.IsChecked = forum.IsSelected;
                        cb.Checked += new RoutedEventHandler(AddForum);
                        cb.Unchecked += new RoutedEventHandler(RemoveForum);

                        panel.Children.Add(cb);
                        var textBox = new TextBlock { Text = forum.LongName, Tag = forum.ShortName };

                        textBox.MouseLeftButtonDown += TreeViewItem_Selected;
                        
                        textBox.Foreground = Brushes.White;
                        panel.Children.Add(textBox);

                        //forumItem.Items.Add(panel);

                        item.Items.Add(panel);
                    }
                    stackPanel1.Children.Add(item);
                }

                var handler = FinishedLoading;
                if (handler != null)
                    handler(this, new ViewSelectedForumsEventArgs { SelectedForums = availableForums.SelectMany(cat => cat.Forums).Where(f => f.IsSelected) });
            }
        }

        void RemoveForum(object sender, RoutedEventArgs e)
        {
            var checkbox = sender as CheckBox;
            var forum = checkbox.Tag as Forum;
            var handler = ForumUnsubscribed;
            if (handler != null)
                handler(this, new ForumSubscribeEventArgs { Forum = forum });
        }

        void AddForum(object sender, RoutedEventArgs e)
        {
            var checkbox = sender as CheckBox;
            var forum = checkbox.Tag as Forum;
            OnForumSubscribed(forum);
        }

        void OnForumSubscribed(Forum forum)
        {
            var handler = ForumSubscribed;

            if (handler != null)
                handler(this, new ForumSubscribeEventArgs { Forum = forum });
        }

        private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
        {
            TextBlock item = (TextBlock)sender;
            ForumSelectedEventArgs args = new ForumSelectedEventArgs { ForumLongName = item.Text.ToString(), ForumShortName = item.Tag.ToString() };
            OnForumSelected(args);
        }

        private void OnForumSelected(ForumSelectedEventArgs e)
        {
            var handler = ForumSelected;
            if (handler != null)
                handler(this, e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BindData(true);
        }
    }
}
